#!/bin/env bash

mysql_dir=/data/mysql5.7

rm_mariadb(){
    echo "remove mariadb"
    yum -y remove mariadb-server
    yum -y remove mariadb-libs
}

mk_mysql_dir(){
    echo "make mysql dirs"
    mkdir -p "$1"/mysql-data
    mkdir -p "$1"/mysql
    mkdir -p "$1"/mysql-binlog
}

add_mysql_usr(){
    if [ "" == "$(awk -F ":" '{print $1}' /etc/passwd | grep mysql)" ]; then
        echo "add mysql user"
        useradd mysql
    fi
}

change_own(){
    echo "chown dir to mysql"
    chown -R mysql.mysql "$1"/*
}


clear_mysql_data(){
    echo "clear mysql data dir"
    \rm -rf "$1"/mysql-data/*
}

initialize_mysql_with_nopwd(){
    echo "initialize mysqld with --initialize-insecure"
    mysqld --initialize-insecure --user=mysql --basedir="$1"/mysql --datadir="$1"/mysql-data
}

initialize_mysql(){
    mysqld --initialize --user=mysql --basedir="$1"/mysql --datadir="$1"/mysql-data &> initial.log
}

show_temp_pwd(){
    awk '{print $0}' initial.log | grep root | awk '{print $10,$11}'
}


create_mycnf(){
    echo "write /etc/my.cnf"
    if [ ! -f "/etc/my.cnf" ]; then
cat << EOF >/etc/my.cnf
[mysqld]
user=mysql
basedir="$1"/mysql 
datadir="$1"/mysql-data
server-id=$(date '+%H%M%S')
port=3306
socket=/tmp/mysql.sock
bind-address=0.0.0.0
log_bin="$1"/mysql-binlog/mysql_bin
[mysql]
socket=/tmp/mysql.sock
prompt=3306 [\\d]>
EOF
    fi
}


create_mysqld_svc(){
    echo "write /etc/systemd/system/mysqld.service"
    if [ ! -f "/etc/systemd/system/mysqld.service" ]; then
cat << EOF >/etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Community Server
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target
Alias=mysql.service

[Service]
User=mysql
Group=mysql

# Start main service
ExecStart="$1"/mysql/bin/mysqld --defaults-file=/etc/my.cnf

LimitNOFILE=5000
EOF
    fi
}


enable_and_start_mysqld(){
    echo "start mysql..."
    systemctl enable mysqld && systemctl start mysqld
}

initialize_mysql_root_pwd(){
    echo "initialize mysql root pwd"
    mysqladmin -uroot -p password root
}

allow_remote_sql(){
    echo "allow root remote access:"
    echo ">use mysql"
    echo ">grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;"
    echo ">flush privileges;"
    echo "forget root pwd:"
    echo "append skip-grant-tables to /etc/my.cnf & restart mysqld"
    echo ">update user set authentication_string = password('root') where user='root'"
}



usage(){
    echo "useage:"
    echo "-h help"
    echo "-m path of mysql"
    exit 0
}

profile_path=/etc/profile

mysql_env(){
    #export PATH=/data/mysql5.7/mysql/bin:$PATH
    echo "$1"
    {
        echo "#MySQL"
        echo "export PATH=$1/mysql/bin:\$PATH"
    } >> ${profile_path}
}


setup_mysql(){
    mysql_env "$1"
    rm_mariadb
    mk_mysql_dir "$1"
    add_mysql_usr
    change_own "$1"
    clear_mysql_data "$1"
    mysql_env
    create_mycnf "$1"
    create_mysqld_svc "$1"
    enable_and_start_mysqld
    initialize_mysql_root_pwd
    allow_remote_sql
}

while getopts 'hm:' OPT;do
    case $OPT in
        m) setup_mysql "$OPTARG";;
        h) usage;;
        ?) usage;;
    esac
done



